﻿using System.Collections;
using System.Data;

namespace Core.Utilities
{
    public class ArrayUtil
    {
        /// <summary>
        /// Splits the string to array list.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="delimeter">The delimeter.</param>
        /// <returns></returns>
        public static ArrayList SplitStringToArrayList(string source, char delimeter)
        {
            if (string.IsNullOrEmpty(source))
            {
                return null;
            }

            ArrayList list = new ArrayList();
            list.AddRange(source.Split(delimeter));

            return list;
        }

        /// <summary>
        /// Values the exists in array.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="source">The source.</param>
        /// <returns></returns>
        public static bool ValueExistsInArray(int value, int[] source)
        {
            if (source == null)
            {
                return false;
            }

            bool result = false;
            for (int i = 0; i < source.Length; i++)
            {
                if (source[i] == value)
                {
                    result = true;
                    break;
                }
            }

            return result;
        }

        /// <summary>
        /// Values the exists in array.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="source">The source.</param>
        /// <returns></returns>
        public static bool ValueExistsInArray(string value, string[] source)
        {
            if (source == null)
            {
                return false;
            }

            bool result = false;
            for (int i = 0; i < source.Length; i++)
            {
                if (source[i] == value)
                {
                    result = true;
                    break;
                }
            }

            return result;
        }

        /// <summary>
        /// Gets the index array of group column.
        /// </summary>
        /// <param name="dt">The dt.</param>
        /// <param name="columnPKName">Name of the column PK.</param>
        /// <returns></returns>
        public static int[] GetIndexArrayOfGroupColumn(DataTable dt, params string[] columnPKName)
        {
            ArrayList al = new ArrayList();

            if (dt.Rows.Count > 0)
            {
                string currentPK = "";
                for (int pkIndex = 0; pkIndex < columnPKName.Length; pkIndex++)
                {
                    currentPK += dt.Rows[0][columnPKName[pkIndex]].ToString();
                }

                for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
                {
                    if ((rowIndex + 1) < dt.Rows.Count)
                    {
                        string nextPK = "";
                        for (int jPK = 0; jPK < columnPKName.Length; jPK++)
                        {
                            nextPK += dt.Rows[rowIndex + 1][columnPKName[jPK]].ToString();
                        }

                        if (nextPK != currentPK)
                        {
                            al.Add(rowIndex);
                            currentPK = nextPK;
                        }
                    }
                    else
                    {
                        // Add last row
                        al.Add(rowIndex);

                    }

                }

                int[] intArray = (int[])al.ToArray(typeof(int));

                return intArray;
            }
            return null;
        }

        /// <summary>
        /// Gets the keys.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <returns></returns>
        public static ArrayList GetKeys(Hashtable table)
        {
            return (new ArrayList(table.Keys));
        }

        /// <summary>
        /// Gets the values.
        /// </summary>
        /// <param name="table">The table.</param>
        /// <returns></returns>
        public static ArrayList GetValues(Hashtable table)
        {
            return (new ArrayList(table.Values));
        }
    }
}
